<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['username'])) {

            //$this->error('页面加载中。。。',U('Login/Login'),1);
            $this->redirect('Login/login');

        }
        $table1="index";
        $this->assign('table',$table1);
    }

    public function Index()
    {
        $map['status'] = 6;      //支付宝成功之后的回调
        //$map['fs'] = 'buy';
        $order = M('xllog');

        //历史总成交
        $z_where = $map;
        $z_where['siji'] = array('gt','0');
        $z_order = $order->where($z_where)->count();
        $z_price = $order->where($z_where)->sum('total_price');



        //日交易统计
        $stime1 = strtotime(date('Y-m-d 00:00:00'));
        $etime1 = strtotime(date('Y-m-d 23:59:59'));
        $where1 = $map;
        $where1['siji'] = array('between', '' . $stime1 . ',' . $etime1 . '');
        $order1 = $order->where($where1)->count();
        $price1 = $order->where($where1)->sum('total_price');


        //昨日交易统计
        $stime2 = strtotime(date('Y-m-d')) - 86400;
        $etime2 = strtotime(date('Y-m-d')) - 1;
        $where2 = $map;
        $where2['siji'] = array('between', '' . $stime2 . ',' . $etime2 . '');
        
        $order2 = $order->where($where2)->count();
        $price2 = $order->where($where2)->sum('price');
        if(empty($price2)){
            $price2=0.00;
        }
        //周交易统计
        //当前日期

        $sdefaultDate = date("Y-m-d");

        //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期

        $first = 1;

        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6

        $w = date('w', strtotime($sdefaultDate));

        //获取本周开始日期，如果$ws是0，则表示周日，减去 6 天

        $wstime3 = date('Y-m-d', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));

        //本周结束日期

        $wetime3 = date('Y-m-d', strtotime("$wstime3 +6 days"));
        $stime3=strtotime($wstime3);
        $etime3=strtotime($wetime3);
        $where3 = $map;
        $where3['siji'] = array('between', '' . $stime3 . ',' . $etime3 . '');
        $order3 = $order->where($where3)->count();
        $price3 = $order->where($where3)->sum('price');

        //月交易统计
        $stime4 = mktime(0, 0, 0, date("m"), 1, date("Y"));
        $etime4 = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
        $where4 = $map;
        $where4['siji'] = array('between', '' . $stime4 . ',' . $etime4 . '');
        $order4 = $order->where($where4)->count();
        $price4 = $order->where($where4)->sum('price');
        //近7日交易走势
//        $stime5=strtotime("-8days");
//        $etime5=strtotime("-1days");
//        $where5['addtime'] = array('between', '' . $stime5 . ',' . $etime5 . '');
//        $order5 = $order->where($where5)->select();
        for($i=7;$i>0;$i--) {
            $dates[] = date('Y-m-d', strtotime("-".$i." days"));
            $s6=strtotime(date('Y-m-d', strtotime("-".$i." days")));
            $e6=strtotime(date('Y-m-d', strtotime("-".$i." days")))+86399;
            $where6 = $map;
            $where6['siji'] = array('between', '' . $s6 . ',' . $e6 . '');
            $ssprice=$order->where($where6)->sum('price');

            $price[] = $ssprice?$ssprice:0;
            $orders[] = $order->where($where6)->count();

        }$dated=array_merge(array_unique($dates));
        $tongji2 = array('riqi' => json_encode($dated), 'order' => json_encode($orders), 'price' => json_encode($price));


        $zuihou =round($z_price/$z_order,2);
        $this->assign('z_order', $z_order);
        $this->assign('zuihou', $zuihou);
        $this->assign('z_price', $z_price);
        $this->assign('order1', $order1);
        $this->assign('price1', $price1);
        $this->assign('order2', $order2);
        $this->assign('price2', $price2);
        $this->assign('order3', $order3);
        $this->assign('price3', $price3);
        $this->assign('order4', $order4);
        $this->assign('price4', $price4);
        $this->assign('tongji2',$tongji2);
        $this->display();

    }

    public function header()
    {

        $user = M('sysuser');
        $map['username'] = session('username');
        $usinfo = $user->where($map)->find();

        return $usinfo;


    }

    public function left()
    {

        $this->display();

    }




}